package com.example.demo.web.rest.controller;

import com.ffcs.oss.param.out.ServiceResp;
import com.example.demo.service.FileService;
import com.example.demo.web.rest.evt.attachment.FileInfoEvt;
import com.example.demo.web.rest.evt.attachment.ObjEvt;
import com.example.demo.web.rest.vm.attachment.FileInfoVm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Desc
 * @Author zhengmf
 * @Date 2019-09-02 11:18:18
 */
@Api("文件上传下载接口")
@RestController
@RequestMapping(value = "/api/file")
public class FileController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private FileService fileService;

    @ApiOperation("获取文件服务器地址")
    @PostMapping(value = "/getFileServerUrl")
    public ServiceResp getFileServerUrl(@RequestParam("key") String key){
        return ServiceResp.getInstance(String.class).success(fileService.getFileServerUrl(key),"获取文件服务器地址成功");
    }

    @ApiOperation("保存文件上传信息")
    @PostMapping(value = "/saveFileInfo")
    public ServiceResp saveFileInfo(@RequestBody FileInfoEvt evt){
        logger.info("保存文件上传信息入参==========="+evt.toString());
        fileService.saveFileInfo(evt);
        return ServiceResp.getInstance().success("保存成功");
    }

    @ApiOperation("根据对象获取附件列表")
    @PostMapping(value = "/getFileByObj")
    public ServiceResp getFileByObj(@RequestBody ObjEvt evt){
        return ServiceResp.getInstance(List.class).success(fileService.getFileByObj(evt));
    }

    @ApiOperation("根据文件ID获取文件路径")
    @PostMapping(value = "/getFileByFileId")
    public ServiceResp getFileByFileId(@RequestParam("fileId") String fileId){
        return ServiceResp.getInstance(FileInfoVm.class).success(fileService.getFileByFileId(fileId));
    }

    @ApiOperation("附件下载")
    @PostMapping(value = "/downloadFile")
    public ServiceResp downloadFile(HttpServletResponse response,
                                    @RequestParam("fileId") String fileId){
        return fileService.downloadFile(response,fileId);
    }

    @ApiOperation("附件上传")
    @PostMapping(value = "/uploadFile")
//    @LogIgnore
    public ServiceResp uploadFile(MultipartHttpServletRequest multipartRequest,FileInfoEvt fileEvt){
        return ServiceResp.getInstance(List.class).success(fileService.uploadFile(multipartRequest,fileEvt),"上传成功");
    }


    @ApiOperation("删除文件")
    @PostMapping(value = "/delFile")
    public ServiceResp delFile(@RequestParam("delFileIds") String delFileIds){
        fileService.delFile(delFileIds);
        return ServiceResp.getInstance(String.class).success("删除成功","");
    }
}
